<?php
App::uses('AppController', 'Controller');
/**
 * Products Controller
 *
 * @property Product $Product
 * @property PaginatorComponent $Paginator
 */
class ProductsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');
	public $uses = array('User', 'Product', 'UserProduct');
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$condition = array(
			'conditions' => array('status' => array(0,1)),
			'order' => array(
				'Product.active_time' => 'DESC'
			)
		);

        $product = $this->Product->find('first', $condition);
        if($product) {
            //get user join this product
            $this->UserProduct->recursive = 1;
            $userProduct = $this->UserProduct->find('all', array('conditions' => array('UserProduct.product_id' => $product['Product']['product_id']), 'limit' => 5));
            
            $this->set('userProduct', $userProduct);
            
            //update status active for this product
            $this->Product->id = $product['Product']['product_id'];
            $this->Product->save(array('status'=>1, 'active_time'=>date('Y-m-d H:i:s')));
		}

		$this->set('product', $product);
	}
	
	public function getUserLogin() {
		$this->layout = 'ajax';
        
        //get user join this product
        $this->UserProduct->recursive = 1;
        $userProduct = $this->UserProduct->find('all', array('conditions' => array('UserProduct.product_id' => $this->request->data['product_id']), 'limit' => 5));
        
        $this->set('userProduct', $userProduct);
		$this->render('/Elements/user_list');
	}

	public function getUserWin($product) {
		//get user login for this product
		$this->UserProduct->recursive = -1;
		$userProduct = $this->UserProduct->find('all', array(
                'conditions' => array('UserProduct.product_id' => $product['Product']['product_id']),
                'order' => 'created_date ASC',
				'limit' => 5
            )
        );
	
		$price = $product['Product']['price'];
		$filter1 = array();
		$filter2 = array();
        if(count($userProduct)) {
            foreach($userProduct as $key=>$row) {
                $minPrice = $row['UserProduct']['price_suggest'] - $price;
                if($minPrice > 0) 
                    $filter1[$row['UserProduct']['id']] = $minPrice;
                else 
                    $filter2[$row['UserProduct']['id']] = $minPrice;
            }
            
            if($filter2) $id = array_search(max($filter2), $filter2);
            else $id = array_search(min($filter1), $filter1);
            
            $this->UserProduct->id = $id;
            $this->UserProduct->saveField('win', 1);
        } else {
            return false;
        }
        
		return true;		
	}
    
/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function result() {
		$id = $this->request->data('product_id');
		if (!$this->Product->exists($id)) {
			throw new NotFoundException(__('Invalid product'));
		}
		$options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
        
        $product = $this->Product->find('first', $options);
		$this->set('product', $product);

        if(!$this->getUserWin($product)) {
            $this->Session->setFlash('Chưa chọn giá cho sản phẩm', 'default', array('class' => 'text-danger'), 'result');
            $this->redirect('/products');
        }
        
        //update status active for this product
        $this->Product->id = $id;
        $this->Product->save(array('status'=>2));
        
        //get user login for this product
        $this->UserProduct->recursive = 1;
        $userProduct = $this->UserProduct->find('all', array('conditions' => array('UserProduct.product_id' => $id), 'limit' => 5));
        
        $this->set('userProduct', $userProduct);
	}
	
	public function waiting() {
		$condition = array(
			'conditions' => array('status' => array(0,1)),
			'order' => array(
				'Product.active_time' => 'DESC'
			)
		);

        $product = $this->Product->find('first', $condition);
        if($product) {
            //update status active for this product
            $this->Product->id = $product['Product']['product_id'];
            $this->Product->save(array('status'=>1, 'active_time'=>date('Y-m-d H:i:s')));
		}
	}
	
	public function test() {
		$this->paginate = array(
			'conditions' => array('status' => array(0,1)),
			'order' => array(
				'Product.active_time' => 'DESC'
			),
			'limit' => 1
		);

		$this->Product->recursive = 0;
        $this->set('products', $this->Paginator->paginate('Product'));
	}
}
